﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Atom.Common
{ 

    public class MsgFuncManage : Hashtable
    { 
        /// <summary>
        /// 消息实体类
        /// </summary>
        private static MsgFuncManage instance = null;  

        /// <summary>
        /// 锁对象
        /// </summary>
        private static readonly object objlock = new object();   

        /// <summary>
        /// 得到单一模式的实例
        /// </summary>
        public static MsgFuncManage Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (objlock)
                    {
                        if (instance == null)
                        {
                            instance = new MsgFuncManage();
                        }
                    }
                }

                return instance;
            }

        }


        /// <summary>
        /// 消息增加到MsgFuncEntity中
        /// </summary>
        /// <param name="moduleFuncEntity">消息实体类</param>
        public static void Add(MsgFuncEntity msgFuncEntity)
        {
            lock (objlock)
            {
                //新消息实体
                List<MsgFuncEntity> _MsgFuncEntityList = new List<MsgFuncEntity>();
                //判断用户ID，是否存在新消息
                if (CheckKeyExist(msgFuncEntity.USERID))
                {
                    //得到新消息烈表
                    _MsgFuncEntityList = (List<MsgFuncEntity>)Instance[msgFuncEntity.USERID];
                    //把最新日期的信息保存到索引0位置
                    _MsgFuncEntityList.Insert(0, msgFuncEntity); 
                    //更新新消息列表
                    Instance[msgFuncEntity.USERID] = _MsgFuncEntityList;
                }
                else
                {
                    //如果用户没有新消息，则增加
                    Instance.Add(msgFuncEntity.USERID, _MsgFuncEntityList);
                
                }
            }
        }

        /// <summary>
        /// 根据用户编号和消息编号，移除消息实体
        /// </summary>
        /// <param name="UserId">用户名</param>
        /// <param name="MsgID">消息编号</param>
        public static void RemoveEntityByMsgID(String UserId,int MsgID)
        {

            lock (objlock)
            {
                //判断用户ID，是否存在新消息
                if (CheckKeyExist(UserId))
                {
                    //消息列表
                    List<MsgFuncEntity> _MsgFuncEntityList = ((List<MsgFuncEntity>)Instance[UserId]).Where(o => o.ID != MsgID).ToList();

                    //更新新消息列表
                    Instance[UserId] = _MsgFuncEntityList; 
                }
               
            } 
        
        }


           /// <summary>
        /// 消息到MsgFuncEntity中
        /// </summary>
        /// <param name="moduleFuncEntity">消息实体类</param>
        public static void Update(string UserID,List<MsgFuncEntity> msgFuncEntitys)
        {
            lock (objlock)
            {
                //新消息实体
                List<MsgFuncEntity> _MsgFuncEntityList = new List<MsgFuncEntity>();
                //判断用户ID，是否存在新消息
                if (CheckKeyExist(UserID))
                { 
                    //更新新消息列表
                    Instance[UserID] = msgFuncEntitys;
                }
                else
                {
                    //如果用户没有新消息，则增加
                    Instance.Add(UserID, msgFuncEntitys);

                }
            }


        }
 

        /// <summary>
        /// 判断MsgFuncManage中KEY是否已存在
        /// </summary>
        /// <param name="userid">用户ID</param>
        /// <returns>返回true:表示已存在,返回fale:不存在</returns>
        public static bool CheckKeyExist(string userid)
        {
            lock (objlock)
            {
                return Instance.Contains(userid);
            }
        }

        /// <summary>
        /// 清除MsgFuncManage中，所有元素
        /// </summary>
        public static void ClearMsgFuncManage()
        {
            lock (objlock)
            {
                Instance.Clear();
            }

        }

        /// <summary>
        /// 得到新消息数量
        /// </summary>
        public static int MsgFuncEntityCount(String USERID)
        {
            lock (objlock)
            {
                //判断用户ID，是否存在新消息
                if (CheckKeyExist(USERID))
                { 
                    //消息列表
                    List<MsgFuncEntity>  _MsgFuncEntityList = (List<MsgFuncEntity>)Instance[USERID];

                    return _MsgFuncEntityList.Count;
                }
                else
                {
                    // 没有新消息， 
                    return 0;

                }
            }

        }

        /// <summary>
        /// 根据用户信息，得到消息实体类
        /// </summary>
        /// <param name="userid">用户ID</param>
        /// <returns>消息实体类</returns>
        public static MsgFuncEntity GetMsgFuncEntityByUserId(string userid)
        {
            lock (objlock)
            { 
                if (Instance.ContainsKey(userid))
                {
                    List<MsgFuncEntity> _MsgFuncEntityList = ((List<MsgFuncEntity>)Instance[userid]).OrderByDescending(o=>o.SENDTIME).ToList();

                    if (_MsgFuncEntityList.Count > 0)
                    {
                        return _MsgFuncEntityList[0];
                    }
                    else
                    {
                        return null; 
                    }
                }
                else
                {
                    return null;
                }
            }
        }


    }
}
